/*
 * Date          Author        Note
 * 2014.12.01    wzwz          度目设置
 * 2015.04.28    wzwz          增加纱出度目补正
 * 2015.06.24    wzwz          增加返回按钮
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "lang.h"
#include "resource.h"
#include "func.h"
#include "com.h"
#include "mhbtn.h"
#include "mhlabel.h"
#include "dumu.h"
#include "dmbz.h"
#include "yarn_dmbz.h"

/*
 *
 */

#define DUMU_CAPTION_LEFT    (30)
#define DUMU_CAPTION_TOP     (60)
#define DUMU_CAPTION_WIDTH   (200)
#define DUMU_CAPTION_HEIGHT  (30)



/*
*
*/
static void CreateDumuInterface(HWND hWnd)
{
    CreateWindow (MH_BTN_NAME,
        MW_DMBZ1_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_DMBZ1,
        DUMU_CAPTION_LEFT, DUMU_CAPTION_TOP, DUMU_CAPTION_WIDTH, DUMU_CAPTION_HEIGHT,
        hWnd, 0);
    CreateWindow (MH_BTN_NAME,
        MW_DMBZ2_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_DMBZ2,
        DUMU_CAPTION_LEFT, DUMU_CAPTION_TOP + DUMU_CAPTION_HEIGHT,
        DUMU_CAPTION_WIDTH, DUMU_CAPTION_HEIGHT,
        hWnd, 0);
    CreateWindow (MH_BTN_NAME,
        MW_DMBZ3_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_DMBZ3,
        DUMU_CAPTION_LEFT, DUMU_CAPTION_TOP + DUMU_CAPTION_HEIGHT * 2,
        DUMU_CAPTION_WIDTH, DUMU_CAPTION_HEIGHT,
        hWnd, 0);
    CreateWindow (MH_BTN_NAME,
        MW_DMBZ4_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_DMBZ4,
        DUMU_CAPTION_LEFT, DUMU_CAPTION_TOP + DUMU_CAPTION_HEIGHT * 3,
        DUMU_CAPTION_WIDTH, DUMU_CAPTION_HEIGHT,
        hWnd, 0);
    char esc[64] = {0};
    sprintf(esc, GetString(802), 5);
    CreateWindow(MH_BTN_NAME,
        esc,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_ESC,
        30,
        60 + 30 * 4,
        200,
        30,
        hWnd, 0);
}
/*
 *
 */
static void OnMoveFocus(HWND hWnd, int up)
{
    ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
    if(up)
    {
        if(nID > ID_DMBZ1 && nID <= ID_DMBZ4)
        {
            --nID;
        }
        else if(nID == ID_ESC)
        {
            nID = ID_DMBZ4;
        }
    }
    else
    {
        if(nID >= ID_DMBZ1 && nID < ID_DMBZ4)
        {
            ++nID;
        }
        else if(nID == ID_DMBZ4)
        {
            nID = ID_ESC;
        }
    }
    SetFocusChild(GetDlgItem(hWnd, nID));
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, int nID)
{
    switch(nID)
    {
    case ID_DMBZ1:
        if(STATE_STOP == GetRunState(g_knit_data))
        {
            CreateDmbzWindow(hWnd);
        }
        break;
    case ID_DMBZ2:
        if(STATE_STOP == GetRunState(g_knit_data))
        {
            CreateYarnDmbzWindow(hWnd);
        }
        break;
    case ID_DMBZ3:
        {
        }
        break;
    case ID_DMBZ4:
        {
        }
        break;
    case ID_ESC:
        {
            PostMessage (hWnd, MSG_CLOSE, 0, 0);
        }
        break;
    }
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    if(key == SCANCODE_ESCAPE || key == SCANCODE_5)
    {
        OnBtnCmd(hWnd, ID_ESC);
    }
    else if(key == SCANCODE_1)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_DMBZ1));
        OnBtnCmd(hWnd, ID_DMBZ1);
    }
    else if(key == SCANCODE_CURSORBLOCKDOWN)
    {
        OnMoveFocus(hWnd, FALSE);
    }
    else if(key == SCANCODE_CURSORBLOCKUP)
    {
        OnMoveFocus(hWnd, TRUE);
    }
    else if(key == SCANCODE_ENTER)
    {
        OnBtnCmd(hWnd, GetDlgCtrlID(GetFocusChild(hWnd)));
    }
}
/*
 *
 */
static int DumuWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreateDumuInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.left += 30;
            rect.top += MASTER_TITLE_TOP;
            rect.right -= 30;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_GREEN(hdc));
            DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        DestroyMainWindow(hWnd);
        MainWindowCleanup (hWnd);
        return 0;
    case MSG_KEYDOWN:
        OnKeyDown(hWnd, wParam);
        return 0;
    case MSG_COMMAND:
        OnBtnCmd(hWnd, LOWORD(wParam));
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls (hWnd);
        }
        break;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_MAST]);
        }
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}

HWND CreateDumuWindow(HWND hHosting)
{
    HWND hWnd;
    MAINWINCREATE CreateInfo;

    int x = (CX_SCREEN - MASTER_WND_CX) / 2;
    int y = (CY_SCREEN - MASTER_WND_CY) / 2;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = MW_DUMU_SET_CAPTION;
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = DumuWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + MASTER_WND_CX - 1;
    CreateInfo.by = y + MASTER_WND_CY - 1;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = hHosting;
    hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_DMBZ1));
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    return hWnd;
}
